package com.yz.jdd.module.main.fragment;

import android.content.Context;
import android.os.Bundle;
import androidx.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.trello.rxlifecycle.ActivityEvent;
import com.yz.jdd.R;
import com.yz.jdd.base.activity.BaseFragment;
import com.yz.jdd.base.event.Events;
import com.yz.jdd.base.event.RxBus;
import com.yz.jdd.base.utils.BaseConfigs;
import com.yz.jdd.base.utils.LogUtils;
import com.yz.jdd.base.utils.PreferencesUtils;
import com.yz.jdd.base.utils.StringUtil;
import com.yz.jdd.module.main.adapter.MsgCountAdapter;
import com.yz.jdd.webview.Router;
import com.yz.jdd.webview.entity.MsgCountBean;

import java.util.List;

import rx.functions.Action1;

/**
 * 消息fragment
 * 2019.04.16
 * ljm
 */
public class MessageFragment extends BaseFragment implements View.OnClickListener {

    private Context context;
    ListView listView;
    MsgCountAdapter adapter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_message, container, false);
        listView = view.findViewById(R.id.listView);

        String jsonStr = PreferencesUtils.getString(getActivity(), BaseConfigs.PREF_MSG_COUNT, "");
        if (!StringUtil.isEmpty(jsonStr)) {
            MsgCountBean msgCountBean = new Gson().fromJson(jsonStr, MsgCountBean.class);
            initView(msgCountBean.getAppMsgVoList());
        }
        // 注册待办消息返回数据监听
        RxBus.with(this).setEvent(Events.EVENT_BACKLOG_RESPONSE).setEndEvent(ActivityEvent.DESTROY).onNext(new Action1<Events<?>>() {
            @Override
            public void call(Events<?> events) {
                String jsonStr = events.getContent();
                if (!StringUtil.isEmpty(jsonStr)) {
                    MsgCountBean msgCountBean = new Gson().fromJson(jsonStr, MsgCountBean.class);
                    initView(msgCountBean.getAppMsgVoList());
                }
            }
        }).create();

        return view;
    }

    public void initView(final List<MsgCountBean.AppMsgVoListBean> appMsgVoList) {
        adapter = new MsgCountAdapter(context, appMsgVoList);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Router.startWebView(context, appMsgVoList.get(i).getNextStip());
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        if (isVisible()) {
            // 可见触发刷新
            LogUtils.i("ljm  onResume 可见触发刷新");
            RxBus.getInstance().send(Events.EVENT_BACKLOG_CHANGE, "");
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            LogUtils.i("ljm onHiddenChanged 为隐藏触发刷新");
            RxBus.getInstance().send(Events.EVENT_BACKLOG_CHANGE, "");
        }
    }

    @Override
    public void onClick(View v) {

    }
}
